package com.pudu.pudusclient;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.pudu.mydemo.view.DrawRaderView;
import com.pudu.mydemo.view.rplidar_node_t;
import com.pudu.pdrobot.App;
import com.pudutech.pdrobot.R;

import java.lang.ref.WeakReference;
import java.util.List;

public class ShowRadarActivity extends AppCompatActivity implements View.OnClickListener{

    private MyHandler mHandler;
    DrawRaderView mRabdarView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_radar);

        mHandler = new MyHandler(this);

        Button btn1 = (Button)this.findViewById(R.id.button1);
        Button btn2 = (Button)this.findViewById(R.id.button2);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);

        mRabdarView = (DrawRaderView)this.findViewById(R.id.myview);

        if( App.getInstance().mRadarType == 0) {
            Intent startService = new Intent(this, RadarUsbService.class);
            startService(startService);

            bindService(startService, usbConnection, Context.BIND_AUTO_CREATE);
        }else if(App.getInstance().mRadarType == 1){
            Intent startService = new Intent(this, HLSRadarService.class);
            startService(startService);

            bindService(startService, usbConnection, Context.BIND_AUTO_CREATE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            unbindService(usbConnection);
        }catch(Exception e)
        {
            e.printStackTrace();
        }

    }

    private HLSRadarService hlsUsbService;
    private RadarUsbService usbService;
    private final ServiceConnection usbConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName arg0, IBinder arg1) {
            if(App.getInstance().mRadarType == 0) {
                usbService = ((RadarUsbService.UsbBinder) arg1).getService();
                usbService.setHandler(mHandler);
            }else if(App.getInstance().mRadarType == 1){
                hlsUsbService = ((HLSRadarService.HLSUsbBinder) arg1).getService();
                hlsUsbService.setHandler(mHandler);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            if(App.getInstance().mRadarType == 0) {
                usbService = null;
            }else{
                hlsUsbService = null;
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch(v.getId())
        {
            case R.id.button1:
                if (App.getInstance().mRadarType == 0) {
                    if (usbService != null) {
                        usbService.startScan();
                    }
                } else {
                    if (hlsUsbService != null) {
                        hlsUsbService.startScan();
                    }
                }
                break;
            case R.id.button2:
                if (App.getInstance().mRadarType == 0) {
                    if (usbService != null) {
                        usbService.stopScan();
                    }
                } else {
                    if (hlsUsbService != null) {
                        hlsUsbService.stopScan();
                    }
                }
                break;
        }
    }

    RPLidarData mRPLidarData = RPLidarData.getInstance();
    HLSRadarData mHLSRadarData = HLSRadarData.getInstance();
    private static class MyHandler extends Handler {
        private final WeakReference<ShowRadarActivity> mActivity;

        public MyHandler(ShowRadarActivity activity) {
            mActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case RadarUsbService.MESSAGE_FROM_SERIAL_PORT:
                    try{

                        try {
                            if (mActivity.get() != null
                                    && mActivity.get().isFinishing() == false
                                    && mActivity.get().mHandler != null) {
                                mActivity.get().mHandler.removeMessages(RadarUsbService.MESSAGE_FROM_SERIAL_PORT);
                            }
                        }catch(Exception e)
                        {
                            e.printStackTrace();
                        }

                        List<rplidar_node_t> listdDatas = (List<rplidar_node_t>) msg.obj;


                        if (mActivity.get().isFinishing() == false)
                            mActivity.get().mRabdarView.showData(listdDatas);
                    }catch(Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case HLSRadarService.HLS_MESSAGE_FROM_SERIAL_PORT:
                    try {
                        try {
                            if (mActivity.get() != null
                                    && mActivity.get().isFinishing() == false
                                    && mActivity.get().mHandler != null) {
                                mActivity.get().mHandler.removeMessages(HLSRadarService.HLS_MESSAGE_FROM_SERIAL_PORT);
                            }
                        }catch(Exception e)
                        {
                            e.printStackTrace();;
                        }

                        List<rplidar_node_t> listdDatas = (List<rplidar_node_t>) msg.obj;
                        Log.i("cvcvcvcv", "listdDatas" + listdDatas.size());
                        if (mActivity.get().isFinishing() == false)
                            mActivity.get().mRabdarView.showData(listdDatas);
                    }catch(Exception e) {
                        e.printStackTrace();
                    }

                    break;
            }
        }
    }
}
